﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Variables;
using CommandExecuter;

namespace CommandExecuter
{
    class CommandExecuter
    {
        public static Variable var;
        public static Variables.Array ara;
        public static bool activeif = false;
        public CommandExecuter(ref Variable vr, ref Variables.Array ar)
        {
            var = vr;
            ara = ar;
        }
        public void ExecuteCommand(string line)
        {
            string[] args = line.Split(' ');
            switch (args[0])
            { 
                case "display":
                    Display(args);
                    break;
                case "var":
                    var.AddVar(args[1], args[3]);
                    break;
                case "input":
                    Input(args[1]);
                    break;
                case "if":
                case "IF":
                case "If":
                    IFST(args);
                    break;
                case "EXIT":
                case "exit":
                case "Exit":
                    Environment.Exit(1);
                    break;
                
            }
            if (args[0].StartsWith("#"))
            {
                if (var.ExistsVar(args[0]))
                {
                    var.SetVal(args[0], args[2]);
                }
                else
                {
                    if (args[2].Contains(",") && !args[2].Contains("\\,"))
                    {
                        object[] r = args[2].Split(',');
                        ArrayList s = new ArrayList(r);
                        ara.AddArray(args[0], s);
                    }
                    else
                        var.AddVar(args[0], args[2]);
                }
            }
            if(args[0].Contains("#") && !args[0].Contains("\\#"))
            {
                string[] split = args[0].Split('#');
                object val = "";
                if(args[1].Contains("#") && !args[1].Contains("\\#"))
                    val = var.GetVal(args[1]);
                else
                {
                    for(int a = 2;a<=args.Length-1;a++)
                    {
                        if(val=="")
                            val = val + args[a];
                        else
                            val = val + " " + args[a];
                    }
                }
                ara.SetItem(split[1], split[0], val);
            }
        }
        public static void Display(string[] args)
        { 
            if(args[1].StartsWith("#") && !args[1].Contains("\\#"))
            {
                object ret = var.GetVal(args[1]);
                if(ret!=null)
                    Console.Write(ret.ToString());
            }
            else if (args[1].Contains("#") && !args[1].Contains("\\#") && !args[1].StartsWith("#"))
            {
                string[] sp = args[1].Split('#');
                object ret = ara.GetItem("#" + sp[1], sp[0]);
                Console.Write(System.Convert.ToString(ret));
            }

            else
            {
                string text = "";
                for (int i = 1; i <= args.Length - 1; i++)
                {
                    text = text + args[i];
                }
                Console.WriteLine(text);
            }
        }
        public static void Input(string target)
        {
            string what = Console.ReadLine();
            if (var.ExistsVar(target))
                var.SetVal(target, what);
            else
                var.AddVar(target, what);
        }
        public static void IFST(string[] args)
        {
            string ar1 = "";
            string ar2 = "";
            string then = "";
            string els = "";
            int i = 0; 
            foreach (string arg in args)
            {
                if (arg == "==" || arg == "!=")
                    break;
                else
                    i++;
            }
            if (i <= args.Length - 1)
            {
                int thpos = 0;
                foreach (string arg in args)
                {
                    if (arg == "then" || arg == "Then" || arg == "THEN")
                        break;
                    else
                    {
                        if (thpos <= args.Length - 1)
                            thpos++;
                    }
                }
                int elsepos = 0;
                foreach (string arg in args)
                {
                    if (arg == "ELSE" || arg == "Else" || arg == "else")
                        break;
                    else
                    {
                        if (elsepos <= args.Length - 1)
                            elsepos++;
                    }
                }
                //Je
                for (int ie = 1; ie < i; ie++)
                {
                    ar1 = ar1 + args[ie];
                }
                //#a - Dobre

                for (int ie = i+1; ie > i && ie<thpos; ie++)
                {
                    ar2 = ar2 + args[ie];
                }
                //Console.WriteLine(ar2);

                
                if (args[i] == "==")
                {
                    object ex1;
                    object ex2;
                    if (ar1.StartsWith("#"))
                        ex1 = var.GetVal(ar1);
                    else
                        ex1 = ar1;
                    if (ar2.StartsWith("#"))
                        ex2 = var.GetVal(ar2);
                    else
                        ex2 = ar2;
                    
                    for (int a = thpos + 1; a < elsepos; a++)
                    {
                        if(then!="")
                            then = then + " " + args[a];
                        else
                            then = then + args[a];
                    }
                    for (int a = elsepos + 1; a <= args.Length - 1; a++)
                    {
                        if (els != "")
                            els = els + " " + args[a];
                        else
                            els = els + args[a];
                    }
                    if (ex1.ToString() == ex2.ToString())
                    {
                        CommandExecuter c = new CommandExecuter(ref var, ref ara);
                        c.ExecuteCommand(then);
                    }
                    else
                        ELSE(els);
                    activeif = true;
                }
                if (args[i] == "!=")
                {
                    object ex1;
                    object ex2;
                    if (ar1.StartsWith("#"))
                        ex1 = var.GetVal(ar1);
                    else
                        ex1 = ar1;
                    if (ar2.StartsWith("#"))
                        ex2 = var.GetVal(ar2);
                    else
                        ex2 = ar2;

                    for (int a = thpos + 1; a <= args.Length - 1; a++)
                    {
                        if (then != "")
                            then = then + " " + args[a];
                        else
                            then = then + args[a];
                    }
                    for (int a = elsepos + 1; a <= args.Length - 1; a++)
                    {
                        if (els != "")
                            els = els + " " + args[a];
                        else
                            els = els + args[a];
                    }
                    if (ex1.ToString() != ex2.ToString())
                    {
                        CommandExecuter c = new CommandExecuter(ref var, ref ara);
                        c.ExecuteCommand(then);
                    }
                    else
                        ELSE(els);
                }
 


            }
        }
        public static void ELSE(string els)
        {
                CommandExecuter cm = new CommandExecuter(ref var,ref ara);
                cm.ExecuteCommand(els);
        }
    }
}